package com.example.springbean.component;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Author: suichen
 * @Date: 2022-05-08 21:48
 * @Description: SpringBean
 */
@Component
public class MySpringBean implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {

    public MySpringBean() {
        System.err.println("Bean通过构造方法进行实例化");
    }

    @PostConstruct
    public void initialize(){
        System.err.println("Bean执行PostConstruct方法内容");
    }


    @Override
    public void setBeanName(String name) {
        System.err.println("Bean实现了BeanNameAware接口,执行setBeanName()方法,name = " + name);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.err.println("Bean实现了BeanFactoryAware接口,执行setBeanFactory(),beanFactory=" + beanFactory.getClass().getCanonicalName());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.err.println("Bean实现了ApplicationContextAware接口,调用setApplicationContext()方法，applicationContext=" + applicationContext.getDisplayName());
    }

    @Override
    public void destroy() throws Exception {
        System.err.println("Bean实现了DisposableBean接口,调用destroy()方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.err.println("Bean 实现了InitializingBean接口,调用afterPropertiesSet()方法");
    }
}
